package blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import blog.entity.Category;
import blog.mapper.ArticleCategoryRefMapper;
import blog.mapper.CategoryMapper;
import blog.service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {
	@Resource
	private CategoryMapper categoryMapper;
	@Resource
	ArticleCategoryRefMapper articleCategoryRefMapper;

	public List<Category> listCategory() {
		List<Category> oneCategories=listSub(0);
		for (Category i:oneCategories) {
			i.setListSub(listSub(i.getCategoryId()));
		}
		return oneCategories;
	}

	@Override
	public List<Category> listSub(Integer parentCateId) {
		List<Category> Sub =categoryMapper.listSub(parentCateId);
		for(Category c:Sub) {
			c.setArticleCount(articleCategoryRefMapper.countArticleByCategoryId(c.getCategoryId()));
		}
		return Sub;
	}

	@Override
	public void add(Category category) {
		 categoryMapper.add( category);
		
	}

	@Override
	public void deleteCategory(Integer categoryId) {
		categoryMapper.deleteCategory( categoryId);
		
	}
}
